package kr.hkit.androidcomplete03.ch29;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;

import kr.hkit.androidcomplete03.R;
import kr.hkit.androidcomplete03.ch29.common.Item;
import kr.hkit.androidcomplete03.ch29.common.MyAdapter;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

public class PullParser extends Activity {
	ListView list;
	ProgressDialog progressDlg;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.parser);
		list = (ListView) findViewById(R.id.orderlistview);
		new ParsingXml().execute("order.xml");
	}

	private class ParsingXml extends AsyncTask<String, Void, ArrayList<Item>> {

		@Override
		protected void onPreExecute() {
			progressDlg = ProgressDialog.show(PullParser.this, "Wait",
					"order.xml & order.json Making ...");
		}

		@Override
		protected void onPostExecute(ArrayList<Item> result) {
			MyAdapter adapter = new MyAdapter(PullParser.this, result);
			progressDlg.dismiss();
			list.setAdapter(adapter);
		}

		@Override
		protected ArrayList<Item> doInBackground(String... params) {
			StringBuffer sb = new StringBuffer();
			String str = null;
			try {
				BufferedReader br = new BufferedReader(new InputStreamReader(
						openFileInput(params[0])));
				while ((str = br.readLine()) != null) {
					sb.append(str);
				}
				br.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return parsingXml(sb.toString());
		}

		private ArrayList<Item> parsingXml(String xml) {
			final String TAG="XML PULL Parser";
			ArrayList<Item> arList = new ArrayList<Item>();
			Item item = null;
			boolean initItem = false;

			try {
				XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
				XmlPullParser parser = factory.newPullParser();
				parser.setInput(new StringReader(xml));
				
				int eventType = parser.getEventType();
				
				while(eventType != XmlPullParser.END_DOCUMENT){
					switch(eventType){
					case XmlPullParser.START_DOCUMENT:
						Log.d(TAG, "START_DOCUMENT");
						break;
						
					case XmlPullParser.START_TAG:
						Log.d(TAG, "START_TAG");
						if(parser.getName().equalsIgnoreCase("item")){
							item = new Item();
							initItem = true;
						}
						//속성이 존재한다면 반드시 여기서 추출
						if(parser.getAttributeCount() > 0){
							for(int i = 0; i < parser.getAttributeCount(); i++){
								if(parser.getAttributeName(i).equalsIgnoreCase("price")){
									item.setItemPrice(Integer.parseInt(parser.getAttributeValue(i)));
								} else {
									item.setItemMaker(parser.getAttributeValue(i));
								}
							}
						}
						break;	
						
					case XmlPullParser.TEXT:
						Log.d(TAG, "TEXT");
						if(initItem){
							item.setItemName(parser.getText());
						}
						break;
						
					case XmlPullParser.END_TAG:
						if(parser.getName().equalsIgnoreCase("item")){
							initItem = false;
							arList.add(item);
						}
						Log.d(TAG, "END_TAG");
						break;	
						
					case XmlPullParser.END_DOCUMENT:
						Log.d(TAG, "END_DOCUMENT");
						break;
						
					}
					eventType = parser.next();
				}
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			return arList;
		}

	}
}
